<template>
  <div>
    <el-dialog title="设置" :visible.sync="dialogVisible">
      <el-form ref="form" :model="form" size="mini">
        <el-form-item label="每列行数" :label-width="formLabelWidth">
          <el-input-number :min="1" :max="5" v-model="form.rowCount" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleSubmit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { mapMutations } from 'vuex';
import { getSelectHeight } from '@/assets/js/utils';

export default {
  props: {
    value: {
      type: Boolean,
      default: false,
    },
    quesGroup: {
      type: Object,
      default() {
        return {};
      },
    },
  },
  data() {
    return {
      dialogVisible: false,
      formLabelWidth: '80',
      form: {
        rowCount: 5,
      },
    };
  },
  watch: {
    value(val) {
      this.dialogVisible = val;
    },
    dialogVisible: {
      handler(val) {
        if (val) {
          this.form = this.resetFormData();
        }
        this.$emit('input', val);
      },
      immediate: true,
    },
  },
  methods: {
    ...mapMutations(['updateQuestionGroupItem']),
    resetFormData() {
      const { rowCount } = this.quesGroup;
      return {
        rowCount,
      };
    },
    handleSubmit() {
      const {
        uuid, contents, splitHeight, itemHeight,
      } = this.quesGroup;
      const { rowCount } = this.form;
      this.updateQuestionGroupItem({
        uuid,
        key: 'rowCount',
        value: rowCount,
      });
      const noSplitHeight = splitHeight + itemHeight * rowCount;
      this.updateQuestionGroupItem({
        uuid,
        key: 'noSplitHeight',
        value: noSplitHeight,
      });
      const height = getSelectHeight(contents, rowCount, noSplitHeight, 210 - 2 * 12.8);
      this.updateQuestionGroupItem({
        uuid,
        key: 'height',
        value: height,
      });
      this.updateQuestionGroupItem({
        uuid,
        key: 'totalHeight',
        value: height,
      });
      this.dialogVisible = false;
    },
  },
};
</script>
